package com.nanfang51g3.ckz.ui.money.list;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.adapter.base.DividerItemDecoration;
import com.nanfang51g3.ckz.adapter.ext.MoneyDetailAdapter;
import com.nanfang51g3.ckz.application.CkzApplication;
import com.nanfang51g3.ckz.domain.MoneyDetailBean;
import com.nanfang51g3.ckz.ui.base.IPresenter;
import com.nanfang51g3.ckz.ui.base.Base2Activity;
import com.nanfang51g3.ckz.utils.AppManager;
import com.nanfang51g3.ckz.utils.EasyToast;
import com.nanfang51g3.ckz.utils.KLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by Administrator on 2016/4/19.
 */
public class MoneyDetailActivity extends Base2Activity implements MoneyDetailContract.View {
    MoneyDetailContract.MoneyDetailActionsListener moneyDetailActionsListener;
    ArrayList<MoneyDetailBean> moneyDetailBeenS = new ArrayList<>();
    MoneyDetailAdapter moneyDetailAdapter;
    @Bind(R.id.index_tv_title)
    TextView indexTvTitle;
    @Bind(R.id.money_recycler_view)
    RecyclerView moneyRecyclerView;
    @Bind(R.id.money_ptr_frame)
    PtrClassicFrameLayout moneyPtrFrame;

    boolean more = false;
    int page = 1;
    int limit = 10;
    int id_type;
    @Override
    public IPresenter getPresenter() {
        return null;
    }
    @Override
    public int getContentView() {
        return R.layout.money_detail;
    }

    @Override
    public boolean isShowLeftIcon() {
        return true;
    }

    @Override
    public boolean isShowRightIcon() {
        return false;
    }

    @Override
    public int setLeftIcon() {
        return R.mipmap.ic_accounting_left;
    }

    @Override
    public int setRightIcon() {
        return 0;
    }

    @Override
    public int showTitle() {
        return id_type
                == 2 ? R.string.str_bank_detail : R.string.str_ali_pay_detail;
    }

    @Override
    public void onClickLeft(View v) {
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public void onClickRight(View v) {

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void showProgress() {
        super.showPro(R.string.str_progress_index);
    }

    @Override
    public void dismissProgress() {
        super.dismissPro();

    }

    @Override
    public void showMessage(String message) {
        EasyToast.showShort(this, message);
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showSuccess(List<MoneyDetailBean> remote) {
        if (more) {
            moneyDetailBeenS.addAll(remote);
            moneyDetailAdapter.addList(moneyDetailBeenS);
        } else {
            moneyDetailBeenS.clear();
            moneyDetailBeenS.addAll(remote);
            moneyDetailAdapter.addList(moneyDetailBeenS);
        }
    }


    @Override
    public void onIntent() {
        super.onIntent();
        id_type = getIntent().getIntExtra("id_type", 1);
        KLog.i("Evd", "type=" + id_type);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        moneyDetailActionsListener = DaggerMoneyDetailContractComponent
                .builder()
                .appComponent(CkzApplication.getAppComponent())
                .moneyDetailPresenterModule(new MoneyDetailPresenterModule(this))
                .build().getMoneyDetailPresenter();
        KLog.i("TAG", moneyDetailActionsListener == null ? "moneyDetailActionsListener==null" : "moneyDetailActionsListener!=null");
        ButterKnife.bind(this);
        moneyPtrFrame.setLastUpdateTimeRelateObject(this);
        moneyPtrFrame.setPtrHandler(new PtrDefaultHandler2() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                // TODO Auto-generated method stub
                more = false;
                page = 1;
                /**
                 * @see MoneyDetailPresenter#requestMoneyDetail(int, int)
                 */
                moneyDetailActionsListener.requestMoneyDetail(page, limit,id_type);
                moneyPtrFrame.refreshComplete();
            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                // TODO Auto-generated method stub
                more = true;
                ++page;
                /**
                 * @see MoneyDetailPresenter#requestMoneyDetail(int, int)
                 */
                moneyDetailActionsListener.requestMoneyDetail(page, limit,id_type);
                moneyPtrFrame.refreshComplete();

            }

        });
        // the following are default settings
        moneyPtrFrame.setResistance(1.7f);
        moneyPtrFrame.setRatioOfHeaderHeightToRefresh(1.2f);
        moneyPtrFrame.setDurationToClose(200);
        moneyPtrFrame.setDurationToCloseHeader(1000);
        // default is false
        moneyPtrFrame.setPullToRefresh(false);
        // default is true
        moneyPtrFrame.setKeepHeaderWhenRefresh(true);

        moneyDetailAdapter = new MoneyDetailAdapter(moneyDetailBeenS, id_type);
        moneyDetailAdapter.setActivity(this);
        moneyRecyclerView.setHasFixedSize(true);//固定大小
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        moneyRecyclerView.setLayoutManager(linearLayoutManager);
        //间隔线条
        moneyRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        moneyRecyclerView.setAdapter(moneyDetailAdapter);


        /**
         *网络请求数据
         * @see MoneyDetailPresenter#requestMoneyDetail(int, int)
         */
        moneyDetailActionsListener.requestMoneyDetail(page, limit,id_type);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);//解除绑定，官方文档只对fragment做了解绑
    }
}
